package com.scqkxx.gamebox.activity.register;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.myself.LoginActivity;
import com.scqkxx.gamebox.activity.myself.loginutils.LoginRequestUtils;
import com.scqkxx.gamebox.activity.myself.loginutils.OnRequestListener;
import com.scqkxx.gamebox.databinding.ActivityRegisterPasswordBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.PreferenceUtils;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class RegisterPasswordActivity extends BaseActivity<ActivityRegisterPasswordBinding> {

    @Override
    public int setLayoutId() {
        return R.layout.activity_register_password;
    }

    @Override
    public void init() {
        binding.mTitleView.setOnTitleClickListener((int titleType) -> finish());

        binding.btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = binding.etName.getText().toString();
                if (TextUtils.isEmpty(password) || password.length() < 6) {
                    ToastUtils.showShortToast(RegisterPasswordActivity.this, "密码长度不足6位");
                } else {
                    String first_name = PreferenceUtils.getString("first_name", "");
                    String last_name = PreferenceUtils.getString("last_name", "");
                    String register_email = PreferenceUtils.getString("register_email", "");
                    String checkCode = PreferenceUtils.getString("checkCode", "");

                    Map<String, Object> map = new HashMap<>();
                    map.put("username", register_email);
                    map.put("checkCode", checkCode);
                    map.put("password", password);
                    map.put("first_name", first_name);
                    map.put("last_name", last_name);

                    LoginRequestUtils.registerBySmsCode(RegisterPasswordActivity.this, map, new OnRequestListener() {
                        @Override
                        public void OnVerificationSuccess() {
                            Log.i("LoginRequestUtils", "OnVerificationSuccess");
                        }

                        @Override
                        public void OnVerificationError(String s) {
                            Log.i("LoginRequestUtils", "OnVerificationError" + s);
                        }

                        @Override
                        public void OnSuccer(Map<String, Object> map) {
                            Log.i("LoginRequestUtils", "OnSuccer" + map);

                            try {
                                Map<String, Object> mapData = (Map<String, Object>) map.get("data");
                                String sessionToken = mapData.get("sessionToken") + "";
                                AVUser.becomeWithSessionTokenInBackground(sessionToken, new LogInCallback<AVUser>() {
                                    @Override
                                    public void done(AVUser user, AVException e) {
                                        if (e == null) {
                                            ToastUtils.showShortToast(RegisterPasswordActivity.this, "注册成功");
                                            RegisterPasswordActivity.this.finish();
                                            startActivity(new Intent(RegisterPasswordActivity.this, LoginActivity.class));
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void OnError(String s) {
                            Log.i("LoginRequestUtils", "OnError" + s);
                            ToastUtils.showShortToast(RegisterPasswordActivity.this, s);
                        }

                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.i("LoginRequestUtils", "onSubscribe" + d);
                        }
                    });
                }
            }
        });
    }
}
